/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Params;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class ThreadConnectionSocketSendShell extends Thread implements Runnable {

    public SocketListener socketListener;
    public Socket connectionSocket;
    public PrintWriter bufferedWriter;//ghi chuoi xuat len mang
    public BufferedReader bufferedReader;//doc chuoi tu mang goi ve

    public ThreadConnectionSocketSendShell(Socket socket, SocketListener socketListener) {
        this.connectionSocket = socket;
        this.socketListener = socketListener;
        try {
            this.bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            this.bufferedWriter = new PrintWriter(connectionSocket.getOutputStream(), true);
        } catch (Exception e) {
            System.out.println(e);
        }
        start();
    }

    @Override
    public void run() {
        while (true) {
            try {
                ChatMessage message = new ChatMessage();
                message.setType(5);
                message.setName(String.valueOf(this.connectionSocket.getPort()));
                message.setMessage(this.bufferedReader.readLine().toString());
//                    if(!message.getMessage().equals("")){
//                        //hien thi thong bao
//                        OptionDialog.showDialogSendMessages(message.getMessage());
//                    }
                socketListener.onRead(message);
            } catch (Exception e) {
                System.out.println(e);
            }
            //lenh nay thong bao cho scheduler biet(dag ranh) de thuc thi 1 thread khac
            Thread.yield();
        }
    }
}
